package com.edge.pbx.view.home;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 */
public class HomeIconLayout extends LinearLayout {
    public HomeIconLayout(Context context) {
        super(context);
    }

    public HomeIconLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HomeIconLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int height = MeasureSpec.getSize(heightMeasureSpec);
        final int width = (int) (height * 8.8f);
        // 1，   1，   1，  1，  4.8
        View view = null;
        int i = 0;
        for ( int  count = getChildCount()-1; i  < count ; i++){
            view = getChildAt(i);
            view.measure(MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
        }
        view = getChildAt(i);
        view.measure(MeasureSpec.makeMeasureSpec((int) (height*4.8f),MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        super.onLayout(changed, l, t, r, b);
        final int height = b - t;
        View view = null;
        int i = 0;
        int start = 0;
        for ( int  count = getChildCount()-1; i  < count ; i++){
            view = getChildAt(i);
            start = (int) (i*(view.getMeasuredWidth()  ));
            view.layout( start,0, start+ view.getMeasuredWidth(), view.getMeasuredHeight()      );
        }
        view = getChildAt(i);
        start = (int) (4*(height  ));
        view.layout(start, 0, start+ view.getMeasuredWidth(), height);

    }
}
